import {Component, OnInit} from '@angular/core';
import {AddGooAttributeValueComponent} from '../add-goo-attribute-value.component';
import {ErrRes} from '../../../../../frame/global/err-res';
import {GooAttributeValue} from '../../goo-attribute-value.type';
import {Services} from '../../../../../frame/module/services';
import {HttpRequestClient} from '../../../../../frame/http-request-client/http-request-client';
import {UrlRou} from '../../../../../frame';
import {Zero3Enum} from '../../../../../frame/enum/zero3-enum';
import {filter, map} from 'rxjs/operators';
import {isNullOrUndefined} from 'util';

@Component({
    selector: 'app-main-add-goo-attribute-value',
    templateUrl: './main-add-goo-attribute-value.component.html',
    styleUrls: ['./main-add-goo-attribute-value.component.css']
})
export class MainAddGooAttributeValueComponent implements OnInit {

    errRes = new ErrRes();

    domain = new GooAttributeValue();

    extend = {
        domain: {
            default: () => {
                this.domain.serAudit = Zero3Enum.AUDIT_FLAG_WAIT_SUBMIT;
                this.domain.serUsing = Zero3Enum.ENABLE;
            },

            gooAttribute: {
                items: [],
                nickname: '所属属性',
                change: (event) => {
                    this.domain.upper = event.id;
                    console.log(event);
                }

            }
        },
    };

    func = {
        save: {
            req: (event) => {
                Services.gooAttributeValue.save(this.domain, this.func.save.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    HttpRequestClient.router.navigate([UrlRou.gooAttributeValue.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        },
        back: {
            req: (event) => {
                HttpRequestClient.router.navigate([UrlRou.gooAttributeValue.list]);
                console.log('fun back');
            }
        },

        loadData: () => {
            Services.gooAttribute.list()
                .pipe(filter(item => !isNullOrUndefined(item) && item.code === 'success'),
                    map(item => {
                        const result = [];
                        item.page.content.forEach(base => {
                            result.push({id: base.uuid, name: base.name});
                        });
                        return result;
                    }))
                .subscribe(result => {
                    this.extend.domain.gooAttribute.items = result;
                });
        }
    };

    constructor(public addGooAttributeValue: AddGooAttributeValueComponent) {
        this.addGooAttributeValue.main = this;
    }

    ngOnInit() {
        this.extend.domain.default();
        this.func.loadData();
    }


}
